/*
* ChooseMessage.java
*
* Created on 08-sep-2011, 15:06:58
*/
package totalpos;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Saúl Hidalgo
*/
public class ChooseMessage extends javax.swing.JInternalFrame {
boolean isOk = false;
List<String> msg2poses;
/** Creates new form ChooseMessage */
protected ChooseMessage() {
try {
System.out.println("[" + Shared.now() + "] " + this.getClass().getName() + " " + Shared.lineNumber() + " Invocado constructor");
initComponents();
updateAll();
isOk = true;
System.out.println("[" + Shared.now() + "] " + this.getClass().getName() + " " + Shared.lineNumber() + " Objecto creado satisfactoriamente");
} catch (SQLException ex) {
Logger.getLogger(ChooseMessage.class.getName()).log(Level.SEVERE, null, ex);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
posMsg = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
msgTable = new javax.swing.JTable();
cancelButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
deleteButton = new javax.swing.JButton();
addButton = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setTitle("Seleccione el mensaje para el cajero ");
posMsg.setFont(new java.awt.Font("Courier New", 1, 18)); // NOI18N
posMsg.setText("Mensaje para el Cajero");
posMsg.setName("posMsg"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
msgTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null},
{null},
{null},
{null}
},
new String [] {
"Mensajes"
}
) {
Class[] types = new Class [] {
java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
msgTable.setName("msgTable"); // NOI18N
msgTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(msgTable);
msgTable.getColumnModel().getColumn(0).setPreferredWidth(130);
cancelButton.setText("Cerrar");
cancelButton.setName("cancelButton"); // NOI18N
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
saveButton.setText("Guardar");
saveButton.setName("saveButton"); // NOI18N
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
deleteButton.setText("Eliminar");
deleteButton.setName("deleteButton"); // NOI18N
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
addButton.setText("Agregar");
addButton.setName("addButton"); // NOI18N
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(posMsg, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(posMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(saveButton)
.addComponent(deleteButton)
.addComponent(addButton))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
int n = msgTable.getSelectedRow();
if ( n != -1){
DefaultTableModel model = (DefaultTableModel) msgTable.getModel();
model.removeRow(msgTable.getSelectedRow());
}
System.out.println("[" + Shared.now() + "] " + this.getClass().getName() + " " + Shared.lineNumber() + " Eliminado item " + n);
}//GEN-LAST:event_deleteButtonActionPerformed
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
DefaultTableModel model = (DefaultTableModel) msgTable.getModel();
model.setNumRows(model.getRowCount()+1);
System.out.println("[" + Shared.now() + "] " + this.getClass().getName() + " " + Shared.lineNumber() + " Agregado Item");
}//GEN-LAST:event_addButtonActionPerformed
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
try {
System.out.println("[" + Shared.now() + "] " + this.getClass().getName() + " " + Shared.lineNumber() + " Guardar todo");
boolean showed = false;
for (int i = 0 ; i < msgTable.getRowCount() ; i++ ){
String s = (String) msgTable.getValueAt(i, 0);
if ( s == null || s.length() < 2){
MessageBox msg = new MessageBox(MessageBox.SGN_CAUTION, "Los mensajes no pueden quedar vacíos!");
msg.show(this);
return;
}
if ( s.length() > Integer.parseInt(Shared.getConfig("maximumLenghtMsg2Pos")) ){
if ( !showed ){
MessageBox msg = new MessageBox(MessageBox.SGN_CAUTION, "Hay mensajes que han sido truncados a " +
Integer.parseInt(Shared.getConfig("maximumLenghtMsg2Pos"))+ " carácteres.");
msg.show(this);
showed = true;
}
msgTable.setValueAt(s.substring(0, Integer.parseInt(Shared.getConfig("maximumLenghtMsg2Pos"))), i, 0);
}
}
ConnectionDrivers.deleteAllMsgs();
ConnectionDrivers.createMsgs(msgTable.getModel());
System.out.println("[" + Shared.now() + "] " + this.getClass().getName() + " " + Shared.lineNumber() + " Guardado satisfactoriamente");
MessageBox msg = new MessageBox(MessageBox.SGN_SUCCESS, "Guardado satisfactoriamente");
msg.show(this);
} catch (SQLException ex) {
MessageBox msg = new MessageBox(MessageBox.SGN_CAUTION, "Error con la base de datos",ex);
msg.show(this);
}
}//GEN-LAST:event_saveButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addButton;
private javax.swing.JButton cancelButton;
private javax.swing.JButton deleteButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable msgTable;
private javax.swing.JLabel posMsg;
private javax.swing.JButton saveButton;
// End of variables declaration//GEN-END:variables
private void updateAll() throws SQLException {
System.out.println("[" + Shared.now() + "] " + this.getClass().getName() + " " + Shared.lineNumber() + " creando lista con mensajes");
DefaultTableModel dtm = (DefaultTableModel) msgTable.getModel();
dtm.setRowCount(0);
msg2poses = ConnectionDrivers.getListMsg2Pos();
for (String msg : msg2poses) {
String[] s = {msg};
dtm.addRow(s);
}
}
}